﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using TarreOA.TarreOADAL;
using TarreOA.TarreOAModels;

namespace TarreOA.TarreOABLL {
    public class Flow_WorkProcess_Service {
        /// <summary>
        /// 办理当前工作步骤
        /// </summary>
        /// <param name="newWorkId">所属工作编号</param>
        /// <param name="step">当前步骤</param>
        /// <returns></returns>
        public int UpdateWorkTransactState(int newWorkId, int step) {
            Flow_WorkProcess_DAO fwDAO = new Flow_WorkProcess_DAO();
            string sql = "update Flow_WorkProcess set Prc_State = 2, Start_Time = @curTime where New_Work_Id = @newWorkId and Step_Id = @step";
            Hashtable ht = new Hashtable();
            ht.Add("@curTime", DateTime.Now);
            ht.Add("@newWorkId", newWorkId);
            ht.Add("@step", step);
            return fwDAO.UpdateWorkProcessInfo(sql, ht);
        }

        /// <summary>
        /// 将工作转交下步
        /// </summary>
        /// <param name="newWorkId">办理过程所属工作编号</param>
        /// <param name="step">当前工作步骤</param>
        /// <param name="nextStep">转交的下一步骤</param>
        /// <param name="jingban">所有经办人登录名</param>
        /// <param name="zhuban">主办人登录名</param>
        /// <returns></returns>
        public int SubmitWork(int newWorkId, int step, int nextStep, string[] jingban, string zhuban) {
            Flow_WorkProcess_DAO fwDAO = new Flow_WorkProcess_DAO();
            string sql = "update Flow_WorkProcess set Prc_State = 3, Finish_Time = '" + DateTime.Now.ToString() + "' where New_Work_Id = " + newWorkId.ToString() + " and Step_id = " + step.ToString() + " ";
            if(jingban != null && jingban.Length > 0) {
                for(int i = 0; i < jingban.Length; i++) {
                    if(!jingban[i].Equals(zhuban))
                        sql += "insert into Flow_WorkProcess values(" + newWorkId + "," + nextStep + ",'" + jingban[i] + "',null,null,default,default) ";
                    else
                        sql += "insert into Flow_WorkProcess values(" + newWorkId + "," + nextStep + ",'" + zhuban + "',null,null,default,1) ";
                }
            }
            return fwDAO.UpdateWorkProcessInfo(sql, new Hashtable());
        }

        /// <summary>
        /// 主办人将当前工作委托给别的经办人办理
        /// </summary>
        /// <param name="newWorkId">工作编号</param>
        /// <param name="stepId">办理过程步骤排序号</param>
        /// <param name="consignUser">被委托的人员登录名</param>
        /// <returns></returns>
        public int ConsignOther(int newWorkId, int stepId, string consignUser) {
            Flow_WorkProcess_DAO fwDAO = new Flow_WorkProcess_DAO();
            string sql = "";
            if(stepId == 1) {
                sql = "update Flow_WorkProcess set Prc_User = '" + consignUser + "' where New_Work_Id = " + newWorkId + " and Step_Id = " + stepId;
            } else {
                sql = "update Flow_WorkProcess set Prc_User_Pop = 0 where New_Work_Id = " + newWorkId + " and Step_Id = " + stepId + " and Prc_User_Pop = 1 ";
                sql += "update Flow_WorkProcess set Prc_User_Pop = 1 where New_Work_Id = " + newWorkId + " and Step_Id = " + stepId + " and Prc_User = '" + consignUser + "'";
            }
            return fwDAO.UpdateWorkProcessInfo(sql, new Hashtable());
        }

        /// <summary>
        /// 根据工作编号查看此工作的当前办理进度
        /// </summary>
        /// <param name="workId"></param>
        /// <returns></returns>
        public List<Flow_WorkProcess> FindCurrentOpState(int workId) {
            List<Flow_WorkProcess> list = null;
            Flow_WorkProcess_DAO fwDAO = new Flow_WorkProcess_DAO();
            string sql = "select Flow_WorkProcess.step_Id as 步骤序号, Flow_NewFlowStep.Prc_Name as 步骤名称," 
                    + "Flow_WorkProcess.Start_Time as 办理开始时间, Flow_WorkProcess.finish_time as 办理结束时间,"
                    + "Flow_WorkProcess.Prc_User_Pop as 办理人权限, Flow_WorkProcess.Prc_State as 办理状态,"
                    + "usertb.username as 办理人用户名, usertb.loginname as 办理人登录名 "
                    + "from Flow_NewFlowStep, Flow_WorkProcess, usertb "
                    + "where Flow_WorkProcess.New_Work_id = @wid and usertb.loginname = Flow_WorkProcess.Prc_User "
                    + "and Flow_newFlowStep.sort_Id = Flow_WorkProcess.step_Id "  
                    + "and Flow_NewFlowStep.New_Flow_Id = (select New_Flow_Id from Flow_NewWork where [Id] = @wid) "
                    + "order by Flow_WorkProcess.step_Id asc";
            Hashtable ht = new Hashtable();
            ht.Add("@wid", workId);
            SqlConnection sqlConn = SQLManager.GetConn();
            SqlDataReader sqlDr = fwDAO.FindResultSetByTest(sql, ht, sqlConn);
            try {
                if(sqlDr != null && sqlDr.HasRows) {
                    list = new List<Flow_WorkProcess>();
                    while(sqlDr.Read()) {
                        Flow_WorkProcess fw = new Flow_WorkProcess();
                        fw.Step_id = (int) sqlDr["步骤序号"];
                        fw.Step_name = (string) sqlDr["步骤名称"];
                        fw.Start_time = sqlDr["办理开始时间"] == DBNull.Value ? DateTime.MinValue : (DateTime) sqlDr["办理开始时间"];
                        fw.Finish_time = sqlDr["办理结束时间"] == DBNull.Value ? DateTime.MinValue : (DateTime) sqlDr["办理结束时间"];
                        fw.Prc_userPop = (int) sqlDr["办理人权限"];
                        fw.Prc_state = (int) sqlDr["办理状态"];
                        fw.Prc_user_username = (string) sqlDr["办理人用户名"];
                        fw.Prc_user_loginName = (string) sqlDr["办理人登录名"];
                        list.Add(fw);
                    }
                }
            } finally {
                if(sqlDr != null) {
                    sqlDr.Close();
                    sqlDr.Dispose();
                }
                if(sqlConn != null) {
                    sqlConn.Close();
                    sqlConn.Dispose();
                }
            }
            return list;
        }
    }
}
